/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.admin.bulletin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.admin.bulletin.service.BulletinReadrecordService;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.admin.bulletin.Bulletin;
import com.fuyao.cloud.admin.bulletin.service.BulletinService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * 通知公告
 *
 * @author lyhills
 * @date 2023-06-29 17:38:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/bulletin" )
@Api(value = "bulletin", tags = "通知公告管理")
public class BulletinController {

    private final  BulletinService bulletinService;
    private final BulletinReadrecordService bulletinReadrecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param bulletin 通知公告
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_view')" )
    public R getBulletinPage(Page page, Bulletin bulletin) {
        return R.ok(bulletinService.page(page, Wrappers.query(bulletin)));
    }


    /**
     * 通过id查询通知公告
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        Bulletin bulletin = bulletinService.getById(id);
        Long browseRecordCount = bulletinReadrecordService.countRead(bulletin.getId(), -1L);
        bulletin.setBrowseRecordCount(browseRecordCount);
        return R.ok(bulletin);
    }

    /**
     * 新增通知公告
     * @param bulletin 通知公告
     * @return R
     */
    @ApiOperation(value = "新增通知公告", notes = "新增通知公告")
    @SysLog("新增通知公告" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_add')" )
    public R save(@RequestBody Bulletin bulletin) {
        boolean save = bulletinService.save(bulletin);
        return R.ok(bulletin,"保存成功");
    }

    /**
     * 修改通知公告
     * @param bulletin 通知公告
     * @return R
     */
    @ApiOperation(value = "修改通知公告", notes = "修改通知公告")
    @SysLog("修改通知公告" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_edit')" )
    public R updateById(@RequestBody Bulletin bulletin) {
        return R.ok(bulletinService.updateById(bulletin));
    }

    /**
     * 通过id删除通知公告
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除通知公告", notes = "通过id删除通知公告")
    @SysLog("通过id删除通知公告" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(bulletinService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param bulletin 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_export')" )
    public List<Bulletin> export(Bulletin bulletin) {
        return bulletinService.list(Wrappers.query(bulletin));
    }

    /**
     * 创建通知公告方法
     * @return 通知公告方法
     */
    @GetMapping(value = "/create")
    public R create() {
        return R.ok(this.bulletinService.create());
    }

    @ApiOperation(value = "批量删除通知公告", notes = "批量删除通知公告")
    @SysLog("批量删除通知公告" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return bulletinService.batchDelete(ids);
    }

    @ApiOperation(value = "发布通知公告", notes = "发布通知公告")
    @SysLog("发布通知公告" )
    @PostMapping("/pushBulletin" )
    @PreAuthorize("@pms.hasPermission('bulletin_bulletin_push')" )
    public R pushBulletin(@RequestBody List<Long> ids) {
        return bulletinService.pushBulletin(ids);
    }

    @ApiOperation(value = "获取通知公告", notes = "获取通知公告")
    @PostMapping("/getBulletinList" )
    public R getBulletinList() {
        return R.ok(bulletinService.getBulletinList(),"查询成功");
    }

}
